<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <!-- 准备好一个容器 -->
    <div id="text"></div>

    <!-- 引入react-dom 支持react dom 操作 -->
    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <!-- react 核心库 -->
    <!-- 蒋jsx装换为js  -->
    <script src="../js/babel.min.js"></script>
    <script type="text/babel">
        // const Life = <div>aaaaa</div>
        // console.log(Life);
        // //渲染组件
        class Life extends React.Component {
            //定义状态

            state = { opacity: 1 }

            //卸载组件
            death = () => {
                //清楚定时器
                clearInterval(this.timer)
                ReactDOM.unmountComponentAtNode(document.getElementById('text'))
            }
            //组件将要挂载完毕的钩子
            componentDidMount() {
                this.timer = setInterval(() => {
                    //获取原始状态
                    let { opacity } = this.state
                    //减小0.1
                    opacity -= 0.1
                    if (opacity <= 0) opacity = 1
                    //设置新的透明度
                    this.setState({ opacity })
                }, 200);
            }
            //组件将要挂载的钩子
            componentWillUnmount() {
                //清楚定时器
                clearInterval(this.timer);
            }
            //初始化渲染，状态更新之后           
            render() {
                console.log('render')
                return (
                    <div>
                        <h2 style={{ opacity: this.state.opacity }}>react 学不会怎么办</h2>
                        <button onClick={this.death}>不活了</button>
                    </div>
                )
            }
        }
        ReactDOM.render(<Life />, document.getElementById('text'))
    </script>
</body>

</html> 